package com.newcapec.erp.utils;

import com.newcapec.erp.bean.AdminEntity;
import com.newcapec.erp.bean.UserEntity;
import com.newcapec.erp.main.DataInit;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class XMLFReader {

    //获取XML文档对象
    private static Document getDocument(String fileName){
        SAXReader saxReader = new SAXReader();
        Document document = null;
        try {
            document = saxReader.read(new File("src/"+fileName));
        } catch (DocumentException e) {
            System.out.println("读取初始化配置文件失败！");
        }
        return document;
    }

    //根据类型获取数据
    public static Vector<Vector<String>> getData(int type){
        Document document;
        List<Element> elements = null;
        switch (type){
            case DataInit.TYPE_WAREHOUSE:
                document = getDocument("wareHouses.xml");
                elements = document.selectNodes("/warehouses/warehouse");
                break;
            case DataInit.TYPE_STORE:
                document = getDocument("stores.xml");
                elements = document.selectNodes("/stores/store");
                break;
            case DataInit.TYPE_SYSTEM:
                document = getDocument("system.xml");
                elements = document.selectNodes("/data/users/user");
                break;
            case DataInit.TYPE_USER:
                document = getDocument("admin.xml");
                elements = document.selectNodes("/data/admin");
                break;
            case DataInit.TYPE_CLIENT :
                document = getDocument("client.xml");
                elements = document.selectNodes("/clientEntitys/clientEntity");
                break;
            case DataInit.TYPE_INVENTORY :
                document = getDocument("inventory.xml");
                elements = document.selectNodes("/inventorys/inventory");
                break;
            default:
                return null;
        }

        Vector<Vector<String>> data = new Vector<>();
        for (int i = 0;i < elements.size();i++){
            Vector<String> v = new Vector<>();
            List<Element> eleList = elements.get(i).elements();
            for (Element ele:eleList){
                v.add(ele.getText());
            }
            data.add(v);
        }
        return data;
    }

    //根据类型和页码获取数据
    public static Vector<Vector<String>> getData(Vector<Vector<String>> data,int currentPage,int maxPage) {
        Vector<Vector<String>> pageData = null;
        if(currentPage < maxPage){
            List<Vector<String>> list = data.subList((currentPage - 1) * 15, (currentPage - 1) * 15 + 15);
            pageData = new Vector<>(list);
        }else if(currentPage == maxPage){
            List<Vector<String>> list = data.subList((currentPage - 1) * 15, data.size());
            pageData = new Vector<>(list);
        }
        return pageData;
    }

    //获取管理员数据
    public static AdminEntity getAdmin() {
        //创建一个管理员信息实体类  把从xml中读取的数据给实体 然后用实体去和输入的值进行比对 然后登录
        Document document = getDocument("admin.xml");
        //到Login节点
        List<Element> list = document.selectNodes("/data/admin");
        Element element = list.get(0);
        String adminAn = element.elementText("adminAn");
        String adminPwd = element.elementText("adminPwd");
        String adminName = element.elementText("adminName");
        String adminTel = element.elementText("adminTel");
        String adminMail = element.elementText("adminMail");
        return new AdminEntity(adminAn, adminPwd,adminName,adminTel,adminMail);
    }

    //获取所有系统数据返回一个List集合
    public static List<UserEntity> getAllSystemData() {
        //创建一个集合把用户数据储存进去然后返回
        List<UserEntity> systemData = new ArrayList<>();
        //获取到所有用户
        Document document = getDocument("system.xml");
        List<Element> list = document.selectNodes("/data/users/user");
        for (Element ele : list) {
            String userAn = ele.elementText("userAn");
            String userName = ele.elementText("userName");
            String userOrganize = ele.elementText("userOrganize");
            String userRole = ele.elementText("userRole");
            String CreateTime = ele.elementText("CreateTime");
            String userState = ele.elementText("userState");
            UserEntity systemEntity=new UserEntity(userAn,userName,userOrganize,userRole,CreateTime,userState);
            systemData.add(systemEntity);
        }
        return systemData;
    }

    //获取所有仓库名
    public static List<String> getAllWarehouseNames(){
        Document document= getDocument("wareHouses.xml");
        List<Element> elements = document.selectNodes("/warehouses/warehouse");
        List<String> allWarehouseNames = new ArrayList<>();
        for (int i = 0;i < elements.size();i++){
            allWarehouseNames.add(elements.get(i).elementText("name"));
        }
        return allWarehouseNames;
    }
}
